package com.edwin.WebSample;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.edwin.ws.WebService.WebServiceSample;
import com.edwin.ws.WebService.WebServiceSampleImpl;

@Controller
@RequestMapping("/webService")
public class WebSample {
	@RequestMapping("/sample")
	public String printWelcome(ModelMap model) {
		model.addAttribute("message", "webService例子");
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
		model.addAttribute("date", dateFormat.format(new java.util.Date()));
		return "sample";
	}

	@RequestMapping(value="/add",method = RequestMethod.GET)
	public String printAdd(ModelMap model) {
		return "addPage";
	}
	
	@RequestMapping(value="/userInfo",method = RequestMethod.GET)
	public String printUserInfo(ModelMap model) {
		return "userInfo";
	}
	
	@RequestMapping(value="/addResult",method = RequestMethod.GET)
	public String printAddsult(ModelMap model,@RequestParam Integer num1,@RequestParam Integer num2) {
		WebServiceSample webServiceSample=new WebServiceSampleImpl();
		model.addAttribute("Results",webServiceSample.addService(num1, num2));
		return "addResult";
	}
	
	
	private Logger logger = LoggerFactory.getLogger(WebSample.class);  
	  @RequestMapping(value = "/list", method = RequestMethod.GET)  
	  @ResponseBody  
	  public Map<String, Object> getUserList() {  
	    logger.info("列表");  
	    List<UserModel> list = new ArrayList<UserModel>();  
	    UserModel um = new UserModel();  
	    um.setId("1");  
	    um.setUsername("sss");  
	    um.setAge(222);  
	    list.add(um);  
	    Map<String, Object> modelMap = new HashMap<String, Object>(3);  
	    modelMap.put("total", "1");  
	    modelMap.put("data", list);  
	    modelMap.put("success", "true");  
	    return modelMap;  
	  }  
	  
	  @RequestMapping(value = "/add", method = RequestMethod.POST)  
	  @ResponseBody  
	  public Map<String, String> addUser(@RequestBody UserModel model) {  
	    logger.info("新增");  
	    logger.info("捕获到前台传递过来的Model，名称为：" + model.getUsername());  
	    Map<String, String> map = new HashMap<String, String>(1);  
	    map.put("success", "true");  
	    return map;  
	  }  
}
